#include "ms_cylinder.h"
#ifndef PI
#define UNDEF_PI
#define PI 3.1415926535897932384626433
#endif
/* ******************************************************************* */
cylinder::cylinder( const double r )
{
	P[1] = P[2] = Q[1] = Q[2] = 0.;
	P[0] = -0.5;
	Q[0] = 0.5;
	D[0] = 1.; D[1] = D[2] = 0.;
	N[0] = 1.; N[1] = N[2] = 0.;
	L    = 1.;
	R    = r;
	vol  = L*r*r*PI;
	CalculateMinMax();
}
/* ******************************************************************* */
cylinder::cylinder( const double p[3], const double q[3], const double r )
{
	memcpy(P, p, sizeof(double)*3);
	memcpy(Q, q, sizeof(double)*3);
	
	R    = r;
	D[0] = Q[0]-P[0];
	D[1] = Q[1]-P[1];
	D[2] = Q[2]-P[2];
	L    = VectorNorm( D );
	N[0] = D[0]/L;
	N[1] = D[1]/L;
	N[2] = D[2]/L;
	vol  = L*r*r*PI;
	CalculateMinMax();
}
/* ******************************************************************* */
void   cylinder::Displace( const double dx[] ) {
	P[0]+=dx[0]; P[1]+=dx[1]; P[2]+=dx[2];
	Q[0]+=dx[0]; Q[1]+=dx[1]; Q[2]+=dx[2];
}
/* ******************************************************************* */
void   cylinder::MoveTo( const double xx[] ) {
	const double dx[3] = { xx[0]-P[0], xx[1]-P[1], xx[2]-P[2] };
	Displace(dx);
}
/* ******************************************************************* */
void   cylinder::CalculateMinMax()
{
	xmin = ( (P[0] < Q[0]) ? P[0] : Q[0] );
	xmax = ( (P[0] > Q[0]) ? P[0] : Q[0] );
	ymin = ( (P[1] < Q[1]) ? P[1] : Q[1] );
	ymax = ( (P[1] > Q[1]) ? P[1] : Q[1] );
	zmin = ( (P[2] < Q[2]) ? P[2] : Q[2] );
	zmax = ( (P[2] > Q[2]) ? P[2] : Q[2] );
	const double	dxmax = R * sqrt( 1. - N[0]*N[0] ),
					dymax = R * sqrt( 1. - N[1]*N[1] ),
					dzmax = R * sqrt( 1. - N[2]*N[2] );
	xmin -= dxmax;	xmax += dxmax;
	ymin -= dymax;	ymax += dymax;
	zmin -= dzmax;	zmax += dzmax;
}
/* ******************************************************************* */
void   DrawVoxelImage(	VoxelImage & vx_img,
				const unsigned char col  ) 
{
/*	int				nr	= R / h;
	double			r	= 0.;
	const double	dr	= R/(double)nr;
	double Q[3][3];
	GetRotationMatrix(Q, N);
	for(int ir=0; ir<=nr; ir++)
	{
		const double dphi = 2.*PI*R / h;
		for(int iphi=0;iphi<nphi; iphi++)
		{
		}
	}*/
}
/* ******************************************************************* */
/* ******************************************************************* */
/* ******************************************************************* */
/* ******************************************************************* */
#ifdef UNDEF_PI
#undef PI
#endif
/* ******************************************************************* */
/* ******************************************************************* */
/* ******************************************************************* */
/* ******************************************************************* */
/* ******************************************************************* */

